package se.ramfelt.psnfriends.util;

import junit.framework.TestCase;

public class RandomXorObfuscatorTest extends TestCase {

	public void testRoundtrip() {
		RandomXorObfuscator obfuscator = new RandomXorObfuscator(12);
		assertFalse("secret".equals(obfuscator.perform("secret")));
		assertEquals("secret", obfuscator.reverse(obfuscator.perform("secret")));
	}
	
	public void testSeparateInstances() {
		assertEquals("secret", new RandomXorObfuscator(12).reverse(new RandomXorObfuscator(12).perform("secret")));
	}
	
	public void testDifferentSeedGivesDifferentObfuscations() {
		assertFalse("secret".equals(new RandomXorObfuscator(212).reverse(new RandomXorObfuscator(12).perform("secret"))));
	}
	
	public void testNumberOfDecodedCharsIsCorrect() {
		assertEquals(6, countChars(new RandomXorObfuscator(12).perform("secret"), ','));
	}

	private int countChars(String haystack, char needle) {
		int count = 0;
		for (int i = 0; i < haystack.length(); i++) {
			if (haystack.charAt(i) == needle) {
				count++;
			}
		}
		return count;
	}
}
